!************************************************************************
! Biased one-lag classification error (Keane and Sauer (2009)) 
! The main output of this file is prob_joint_rating used in E_likegaussj
!************************************************************************
MODULE mod_classerror

USE mod_types
USE mod_param_initial
USE mod_parameters

IMPLICIT NONE

CONTAINS


!***********************************************************************************************
!(A) Conditional probability of OBSERVED performance given TRUE performance
!    hk=1 (LOW TYPE)  hk=2 (HIGH TYPE)
!    outcome_o, outcome = 0,1 - observed and true rating
!    NOTES: the formulation follows Hausman, Abrevaya and Scott-Morton (1998) with
!    i.   task  = task in t  
!    ii.  task1 = task in t-1
!    iii. task2 = task in t+1
!***********************************************************************************************
!==================================================
FUNCTION rat1high(ftask, pastrat,pastrat1,ftenure)
!==================================================
INTEGER, INTENT(IN):: pastrat, ftask, pastrat1, ftenure
REAL(dp):: rat1high


rat1high=gammapast11_0+ gammapast11_1 + d1_1*ftenure + d1_3*ftenure**2.0_dp


END FUNCTION rat1high


!===================================================
FUNCTION rat1low(ftask, pastrat, pastrat1,ftenure)
!===================================================
INTEGER, INTENT(IN):: pastrat, ftask, pastrat1,ftenure
REAL(dp):: rat1low


rat1low=gammapast11_0 + d1_1*ftenure + d1_3*ftenure**2.0_dp

END FUNCTION rat1low


!=====================================================
FUNCTION rat2high(ftask, pastrat, pastrat1,ftenure)
!=====================================================
INTEGER, INTENT(IN):: pastrat, ftask, pastrat1,ftenure
REAL(dp):: rat2high

 
  rat2high = gammapast33_0 + gammapast33_1 + d1_2*(ftenure-1) + d1_3*(ftenure-1)**2.0_dp


END FUNCTION rat2high



!=====================================================
FUNCTION rat2low(ftask, pastrat, pastrat1,ftenure)
!=====================================================
INTEGER, INTENT(IN):: pastrat, ftask, pastrat1,ftenure
REAL(dp):: rat2low


 rat2low = gammapast33_0 + d1_2*(ftenure-1) + d1_3*(ftenure-1)**2.0_dp


END FUNCTION rat2low



!=============================================
FUNCTION rat3high(ftask,pastrat, pastrat1,ftenure)
!=============================================
INTEGER, INTENT(IN):: pastrat, ftask, pastrat1,ftenure
REAL(dp):: rat3high


  rat3high = gammapast33_0 + gammapast33_1 + d1_2*ftenure + d1_3*ftenure**2.0_dp



END FUNCTION rat3high


!============================================
FUNCTION rat3low(ftask,pastrat, pastrat1,ftenure)
!============================================
INTEGER, INTENT(IN):: pastrat, ftask, pastrat1,ftenure
REAL(dp):: rat3low


  rat3low = gammapast33_0 + d1_2*ftenure + d1_3*ftenure**2.0_dp


END FUNCTION rat3low


!==========================================================================================
FUNCTION prob_rating(hk,task,task1,task2,prevrat1,prevrat2, outcome_o,outcome,phi,ftenure)
!==========================================================================================

INTEGER, INTENT(IN):: hk, task, task1, task2, outcome_o, outcome, prevrat1, prevrat2, ftenure
REAL(dp), INTENT(IN):: phi
REAL(dp):: prob_rating
REAL(dp), PARAMETER:: maxvalue=180.0_dp



!************************************************
!(1) HIGH TYPE: 
!    B21: base error for hk=2 (high type) at L1
!    B22: base error for hk=2 (high type) at L2
!    B23: base error for hk=2 (high type) at L3
!************************************************
!__________________________

IF(hk==2 .or. hk==1) THEN
!__________________________



!****************************************************
!(1) Conditional prob of the OBSERVED rating at L1
!****************************************************
!----------------------------------------------------------------
!(a) Pr of HIGH signal at L1, given that HIGH signal is realized
!----------------------------------------------------------------
IF (task==1 .and. outcome_o==1 .and. outcome==1) THEN

prob_rating = DEXP(rat1high(task,prevrat1, prevrat2,ftenure))/(1.0_dp+DEXP(rat1high(task,prevrat1, prevrat2,ftenure)))
  

!----------------------------------------------------------------------------
!(b) Pr of LOW signal at L1, given that a HIGH signal is realized (E_1(1,t))
!----------------------------------------------------------------------------
ELSE IF (task==1 .and. outcome_o==0 .and. outcome==1) THEN

prob_rating = 1.0_dp/(1.0_dp+DEXP(rat1high(task,prevrat1, prevrat2,ftenure)))


!----------------------------------------------------------------------------
!(c) Pr of HIGH signal at L1, given that a LOW signal is realized (E_0(1,t))
!----------------------------------------------------------------------------
ELSE IF (task==1 .and. outcome_o==1 .and. outcome==0) THEN

prob_rating = DEXP(rat1low(task,prevrat1, prevrat2,ftenure))/(1.0_dp+DEXP(rat1low(task,prevrat1, prevrat2,ftenure))) 


!----------------------------------------------------------------
!(d) Pr of LOW signal at L1, given that a LOW signal is realized 
!----------------------------------------------------------------
ELSE IF (task==1 .and. outcome_o==0 .and. outcome==0) THEN

prob_rating = 1.0_dp/(1.0_dp+DEXP(rat1low(task,prevrat1, prevrat2,ftenure))) 




!****************************************************
!(2) Conditional prob of the OBSERVED rating at L2
!****************************************************
!----------------------------------------------------------------
!(a) Pr of HIGH signal at L2, given that HIGH signal is realized
!----------------------------------------------------------------
ELSE IF (task==2 .and. outcome_o==1 .and. outcome==1) THEN

prob_rating = DEXP(rat2high(task,prevrat1, prevrat2,ftenure))/(1.0_dp+DEXP(rat2high(task,prevrat1, prevrat2,ftenure)))  


!-----------------------------------------------------------------
!(b) Pr of LOW signal at L2, given that a HIGH signal is realized 
!-----------------------------------------------------------------
ELSE IF (task==2 .and. outcome_o==0 .and. outcome==1) THEN

prob_rating = 1.0_dp/(1.0_dp+DEXP(rat2high(task,prevrat1, prevrat2,ftenure))) 


!-----------------------------------------------------------------
!(c) Pr of HIGH signal at L2, given that a LOW signal is realized
!-----------------------------------------------------------------
ELSE IF (task==2 .and. outcome_o==1 .and. outcome==0) THEN

prob_rating = DEXP(rat2low(task,prevrat1, prevrat2,ftenure))/(1.0_dp+DEXP(rat2low(task,prevrat1, prevrat2,ftenure))) 


!----------------------------------------------------------------
!(d) Pr of LOW signal at L2, given that a LOW signal is realized 
!----------------------------------------------------------------
ELSE IF (task==2 .and. outcome_o==0 .and. outcome==0) THEN

prob_rating = 1.0_dp/(1.0_dp+DEXP(rat2low(task,prevrat1, prevrat2,ftenure))) 





!***************************************************
!(3) Conditional prob of the OBSERVED rating at L3
!***************************************************
!----------------------------------------------------------------
!(a) Pr of HIGH signal at L3, given that HIGH signal is realized
!----------------------------------------------------------------
ELSE IF (task>=3 .and. outcome_o==1 .and. outcome==1) THEN

prob_rating = DEXP(rat3high(task,prevrat1, prevrat2,ftenure))/(1.0_dp+DEXP(rat3high(task,prevrat1, prevrat2,ftenure)))  



!-----------------------------------------------------------------
!(b) Pr of LOW signal at L3, given that a HIGH signal is realized 
!-----------------------------------------------------------------
ELSE IF (task>=3 .and. outcome_o==0 .and. outcome==1) THEN

prob_rating = 1.0_dp/(1.0_dp+DEXP(rat3high(task,prevrat1, prevrat2,ftenure)))  



!-----------------------------------------------------------------
!(c) Pr of HIGH signal at L3, given that a LOW signal is realized
!-----------------------------------------------------------------
ELSE IF (task>=3 .and. outcome_o==1 .and. outcome==0) THEN

prob_rating = DEXP(rat3low(task,prevrat1, prevrat2,ftenure))/(1.0_dp+DEXP(rat3low(task,prevrat1, prevrat2,ftenure))) 


!----------------------------------------------------------------
!(d) Pr of LOW signal at L3, given that a LOW signal is realized 
!----------------------------------------------------------------
ELSE IF (task>=3 .and. outcome_o==0 .and. outcome==0) THEN

prob_rating = 1.0_dp/(1.0_dp+DEXP(rat3low(task,prevrat1, prevrat2,ftenure)))



ELSE
PRINT*, "Error, you haven't specified the conditional rating prob"

END IF



END IF !loop on types

END FUNCTION prob_rating




!********************************************************************************
!(B) Probability of tue performance given a manager's TYPE and LEVEL assignment
!    hk=1 (LOW TYPE), hk=2 (HIGH TYPE), task=1,2,3, outcome=0,1
!********************************************************************************

FUNCTION p_success(hk,task,outcome, priortype)


INTEGER, INTENT(IN):: hk, task, outcome, priortype
REAL(dp):: p_success



!***********
! HIGH TYPE
!***********
!************
!(1) Level 1
!************
!outcome 2 (high)
IF (hk==2 .and. task==1 .and. outcome==1 .and. priortype==1) THEN
p_success = alpha_1

!outcome 1 (low)
ELSE IF  (hk ==2 .and. task==1 .and. outcome==0 .and. priortype==1) THEN
p_success = 1.0_dp - alpha_1


ELSE IF (hk==2 .and. task==1 .and. outcome==1 .and. priortype==2) THEN
p_success = alpha_1

!outcome 1 (low)
ELSE IF  (hk ==2 .and. task==1 .and. outcome==0 .and. priortype==2) THEN
p_success = 1.0_dp - alpha_1



ELSE IF (hk==2 .and. task==1 .and. outcome==1 .and. priortype==3) THEN
p_success = alpha_1

!outcome 1 (low)
ELSE IF  (hk ==2 .and. task==1 .and. outcome==0 .and. priortype==3) THEN
p_success = 1.0_dp - alpha_1


ELSE IF (hk==2 .and. task==1 .and. outcome==1 .and. priortype==4) THEN
p_success = alpha_1

!outcome 1 (low)
ELSE IF  (hk ==2 .and. task==1 .and. outcome==0 .and. priortype==4) THEN
p_success = 1.0_dp - alpha_1




!************
!(2) Level 2
!************
ELSE IF(hk==2 .and. task==2 .and. outcome==1 .and. priortype==1) THEN
p_success = alpha_2

ELSE IF  (hk==2 .and. task==2 .and. outcome==0 .and. priortype==1) THEN
p_success = 1.0_dp - alpha_2


ELSE IF(hk==2 .and. task==2 .and. outcome==1 .and. priortype==2) THEN
p_success = alpha_2

ELSE IF  (hk==2 .and. task==2 .and. outcome==0 .and. priortype==2) THEN
p_success = 1.0_dp - alpha_2


ELSE IF(hk==2 .and. task==2 .and. outcome==1 .and. priortype==3) THEN
p_success = alpha_2

ELSE IF  (hk==2 .and. task==2 .and. outcome==0 .and. priortype==3) THEN
p_success = 1.0_dp - alpha_2



ELSE IF(hk==2 .and. task==2 .and. outcome==1 .and. priortype==4) THEN
p_success = alpha_2

ELSE IF  (hk==2 .and. task==2 .and. outcome==0 .and. priortype==4) THEN
p_success = 1.0_dp - alpha_2





!************
!(3) Level 3
!************
ELSE IF(hk==2 .and. task>=3 .and. outcome==1 .and. priortype==1) THEN
p_success = alpha_3


ELSE IF  (hk==2 .and. task>=3 .and. outcome==0 .and. priortype==1) THEN
p_success = 1.0_dp - alpha_3


ELSE IF(hk==2 .and. task>=3 .and. outcome==1 .and. priortype==2) THEN
p_success = alpha_3


ELSE IF  (hk==2 .and. task>=3 .and. outcome==0 .and. priortype==2) THEN
p_success = 1.0_dp - alpha_3



ELSE IF(hk==2 .and. task>=3 .and. outcome==1 .and. priortype==3) THEN
p_success = alpha_3


ELSE IF  (hk==2 .and. task>=3 .and. outcome==0 .and. priortype==3) THEN
p_success = 1.0_dp - alpha_3




ELSE IF(hk==2 .and. task>=3 .and. outcome==1 .and. priortype==4) THEN
p_success = alpha_3


ELSE IF  (hk==2 .and. task>=3 .and. outcome==0 .and. priortype==4) THEN
p_success = 1.0_dp - alpha_3





!************
!(4) Level 0
!************
ELSE IF(hk==2 .and. task==-2 .and. outcome==1) THEN
!p_success = 0.5_dp
p_success = 1.0_dp


ELSE IF  (hk==2 .and. task==-2 .and. outcome==0) THEN
!p_success = 0.5_dp
p_success = 1.0_dp



!***********
! LOW TYPE
!***********
!************
!(1) Level 1
!************
ELSE IF (hk==1 .and. task==1 .and. outcome==1 .and. priortype==1) THEN
p_success = beta_1


ELSE IF  (hk==1 .and. task==1 .and. outcome==0  .and. priortype==1) THEN
p_success = 1.0_dp - beta_1


ELSE IF (hk==1 .and. task==1 .and. outcome==1 .and. priortype==2) THEN
p_success = beta_1


ELSE IF  (hk==1 .and. task==1 .and. outcome==0  .and. priortype==2) THEN
p_success = 1.0_dp - beta_1



ELSE IF (hk==1 .and. task==1 .and. outcome==1 .and. priortype==3) THEN
p_success = beta_1


ELSE IF  (hk==1 .and. task==1 .and. outcome==0  .and. priortype==3) THEN
p_success = 1.0_dp - beta_1



ELSE IF (hk==1 .and. task==1 .and. outcome==1 .and. priortype==4) THEN
p_success = beta_1


ELSE IF  (hk==1 .and. task==1 .and. outcome==0  .and. priortype==4) THEN
p_success = 1.0_dp - beta_1



!************
!(2) Level 2
!************
ELSE IF(hk==1 .and. task==2 .and. outcome==1 .and. priortype==1) THEN
p_success = beta_2


ELSE IF  (hk==1 .and. task==2 .and. outcome==0 .and. priortype==1) THEN
p_success = 1.0_dp - beta_2


ELSE IF(hk==1 .and. task==2 .and. outcome==1 .and. priortype==2) THEN
p_success = beta_2


ELSE IF  (hk==1 .and. task==2 .and. outcome==0 .and. priortype==2) THEN
p_success = 1.0_dp - beta_2



ELSE IF(hk==1 .and. task==2 .and. outcome==1 .and. priortype==3) THEN
p_success = beta_2


ELSE IF  (hk==1 .and. task==2 .and. outcome==0 .and. priortype==3) THEN
p_success = 1.0_dp - beta_2



ELSE IF(hk==1 .and. task==2 .and. outcome==1 .and. priortype==4) THEN
p_success = beta_2


ELSE IF  (hk==1 .and. task==2 .and. outcome==0 .and. priortype==4) THEN
p_success = 1.0_dp - beta_2




!************
!(3) Level 3
!************
ELSE IF(hk==1 .and. task>=3 .and. outcome==1 .and. priortype==1) THEN
p_success = beta_3


ELSE IF  (hk ==1 .and. task>=3 .and. outcome==0 .and. priortype==1) THEN
p_success = 1.0_dp - beta_3



ELSE IF(hk==1 .and. task>=3 .and. outcome==1 .and. priortype==2) THEN
p_success = beta_3


ELSE IF  (hk ==1 .and. task>=3 .and. outcome==0 .and. priortype==2) THEN
p_success = 1.0_dp - beta_3


ELSE IF(hk==1 .and. task>=3 .and. outcome==1 .and. priortype==3) THEN
p_success = beta_3


ELSE IF  (hk ==1 .and. task>=3 .and. outcome==0 .and. priortype==3) THEN
p_success = 1.0_dp - beta_3



ELSE IF(hk==1 .and. task>=3 .and. outcome==1 .and. priortype==4) THEN
p_success = beta_3


ELSE IF  (hk ==1 .and. task>=3 .and. outcome==0 .and. priortype==4) THEN
p_success = 1.0_dp - beta_3



!************
!(4) Level 0
!************
ELSE IF(hk==1 .and. task==-2 .and. outcome==1) THEN
!p_success = 0.5_dp
p_success = 1.0_dp


ELSE IF  (hk==1 .and. task==-2 .and. outcome==0) THEN
!p_success = 0.5_dp
p_success = 1.0_dp


ELSE 
PRINT*, "Error, you haven't specified a probability of success"

END IF 

END FUNCTION p_success




!******************************************************************************
!(C) Compute the joint probability of observed rating and realized performance
!    prob_rating(hk,task,outcome_o,outcome)
!    p_success(hk,task,outcome)
!******************************************************************************
FUNCTION prob_joint_rating(hk,task,task1,task2,prevrat1,prevrat2,outcome_o,outcome,phi,type_prior,ftenure)


INTEGER, INTENT(IN):: hk, task, task1, task2, prevrat1, prevrat2, outcome_o, outcome, type_prior, ftenure
REAL(dp), INTENT(IN):: phi
REAL(dp):: prob_joint_rating


prob_joint_rating = prob_rating(hk,task,task1,task2, prevrat1,prevrat2, outcome_o,outcome,phi, ftenure)&
                   *p_success(hk,task,outcome,type_prior)



END FUNCTION prob_joint_rating



END MODULE mod_classerror